package somar;

import java.io.IOException;
import java.util.Hashtable;

import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.ServiceReference;

import services.RobotService;
import temperature.TemperatureService;
import sensors.SensorService;

import ch.ethz.iks.r_osgi.RemoteOSGiException;
import ch.ethz.iks.r_osgi.RemoteOSGiService;
import ch.ethz.iks.r_osgi.RemoteServiceReference;
import ch.ethz.iks.r_osgi.URI;

public class GettingRemoteService implements Runnable {

	private BundleContext context;
	private String cIpAddress;
	private int port = 9278;
	
	public GettingRemoteService(BundleContext context, String cIpAddress) {
		this.context = context;
		this.cIpAddress = cIpAddress;
	}
	
	@Override
	public void run() {

		RemoteServiceReference[] rsr = null;
		
		ServiceReference<?> sref = context.getServiceReference(RemoteOSGiService.class.getName());
		if(sref == null) {
			try {
				throw new BundleException("No R-OSGi found");
			} catch (BundleException e) {
				e.printStackTrace();
			}
		}
		
		RemoteOSGiService remote = (RemoteOSGiService) context.getService(sref);
		System.out.println("[SOMAR Server] remote instance : " + remote);
		try {
			rsr = remote.connect(new URI("r-osgi://" + cIpAddress + ":" + port));
		} catch (RemoteOSGiException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		String uriString = "r-osgi://" + cIpAddress + ":" + port;
		System.out.println("[SOMAR Server] URI : " + uriString);
		
		Object rs = remote.getRemoteService(rsr[0]);
		
		if(rs instanceof RobotService) {
			
			RobotService rr = (RobotService)rs;
			Hashtable<String, String> props = new Hashtable<String, String>();
			props.put("SOAP.service.name", RobotService.class.getName());
			context.registerService(RobotService.class.getName(), rr, props);
			
			String names[] = RobotService.class.getName().split("[.]");
			String serviceName = names[names.length-1];
			RSELRepository.putServiceObject(serviceName, rr);
			
//			RSELRepository.putServiceObject(RobotService.class.getName(), rr);
//			System.out.println(rr.move100cm());
			
		} else if (rs instanceof TemperatureService) {

			TemperatureService ts = (TemperatureService)rs;
			Hashtable<String, String> props = new Hashtable<String, String>();
			props.put("SOAP.service.name", TemperatureService.class.getName());
			context.registerService(TemperatureService.class.getName(), ts, props);

			String names[] = TemperatureService.class.getName().split("[.]");
			String serviceName = names[names.length-1];
			RSELRepository.putServiceObject(serviceName, ts);
			
//			RSELRepository.putServiceObject(TemperatureService.class.getName(), ts);
//			System.out.println(ts.getTemperature());

		} else if (rs instanceof SensorService) {
			SensorService ss = (SensorService)rs;
			Hashtable<String, String> props = new Hashtable<String, String>();
			props.put("SOAP.service.name", SensorService.class.getName());
			context.registerService(SensorService.class.getName(), ss, props);
			
			String names[] = SensorService.class.getName().split("[.]");
			String serviceName = names[names.length-1];
			RSELRepository.putServiceObject(serviceName, ss);
			
			
		}
	}
}